{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-11-20T05:41:06.361763400Z",
     "start_time": "2023-11-20T05:41:06.317361Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import re\n",
    "\n",
    "df = pd.read_csv('../static/data/ershou_data.csv')\n",
    "df['region'] = df['address'].apply(lambda x: x.split(',')[1])\n",
    "df['region'] = df['region'].apply(lambda x: x[:2])\n",
    "df['area(㎡)'] = df['huxing'].apply(lambda x: x.split('|')[1][:-1])\n",
    "df['house_type'] = df['huxing'].apply(lambda x: x.split('|')[0])\n",
    "df['orientation'] = df['huxing'].apply(lambda x: x.split('|')[2])\n",
    "df['unit_price'] = df['price'].apply(lambda x: x.split(',')[1][:-3])\n",
    "df['total_price'] = df['price'].apply(lambda x: x.split(',')[0][:-1])\n",
    "df['tag'] = df['tag'].apply(lambda x: \"无\" if pd.isnull(x) else x)\n",
    "df = df[['name', 'region', 'area(㎡)', 'house_type', 'orientation', 'tag', 'unit_price', 'total_price']]\n",
    "df.to_csv('../static/data/pretreatment_SHH_data.csv',index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "           name region area_min area_max house_type                tag  \\\n0      和平碧桂园·星钻     和平       98      159   3室,4室,5室      车位充足,装修交付,大户型   \n1          铭成华府     源城       92      122   2室,4室,5室  车位充足,装修交付,大户型,低总价   \n2        紫金·荣耀城     紫金    92.35   146.72   2室,3室,4室            大户型,低单价   \n3         和平碧桂园     和平      117      145      3室,4室           装修交付,大户型   \n4         坚基风光里     源城       75      130   2室,3室,5室   车位充足,大户型,低总价,低单价   \n..          ...    ...      ...      ...        ...                ...   \n223  仙塘中心市场创富中央     东源        0        0          无       学校,购物中心,配套纯熟   \n224      华达新城商铺     源城        0        0          无                  无   \n225      帝豪国际花园     源城      161      161         4室  装修交付,学校,购物中心,配套纯熟   \n226        中心华府     源城      114      138      3室,4室      车位充足,装修交付,大户型   \n227        华达国际     东源   115.73   141.51      3室,4室        高绿化率,学校,大户型   \n\n     unit_price  \n0        5800.0  \n1        5700.0  \n2        4800.0  \n3        5800.0  \n4        5500.0  \n..          ...  \n223      5647.0  \n224      9900.0  \n225      5800.0  \n226         0.0  \n227      6100.0  \n\n[228 rows x 7 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>name</th>\n      <th>region</th>\n      <th>area_min</th>\n      <th>area_max</th>\n      <th>house_type</th>\n      <th>tag</th>\n      <th>unit_price</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>和平碧桂园·星钻</td>\n      <td>和平</td>\n      <td>98</td>\n      <td>159</td>\n      <td>3室,4室,5室</td>\n      <td>车位充足,装修交付,大户型</td>\n      <td>5800.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>铭成华府</td>\n      <td>源城</td>\n      <td>92</td>\n      <td>122</td>\n      <td>2室,4室,5室</td>\n      <td>车位充足,装修交付,大户型,低总价</td>\n      <td>5700.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>紫金·荣耀城</td>\n      <td>紫金</td>\n      <td>92.35</td>\n      <td>146.72</td>\n      <td>2室,3室,4室</td>\n      <td>大户型,低单价</td>\n      <td>4800.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>和平碧桂园</td>\n      <td>和平</td>\n      <td>117</td>\n      <td>145</td>\n      <td>3室,4室</td>\n      <td>装修交付,大户型</td>\n      <td>5800.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>坚基风光里</td>\n      <td>源城</td>\n      <td>75</td>\n      <td>130</td>\n      <td>2室,3室,5室</td>\n      <td>车位充足,大户型,低总价,低单价</td>\n      <td>5500.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>223</th>\n      <td>仙塘中心市场创富中央</td>\n      <td>东源</td>\n      <td>0</td>\n      <td>0</td>\n      <td>无</td>\n      <td>学校,购物中心,配套纯熟</td>\n      <td>5647.0</td>\n    </tr>\n    <tr>\n      <th>224</th>\n      <td>华达新城商铺</td>\n      <td>源城</td>\n      <td>0</td>\n      <td>0</td>\n      <td>无</td>\n      <td>无</td>\n      <td>9900.0</td>\n    </tr>\n    <tr>\n      <th>225</th>\n      <td>帝豪国际花园</td>\n      <td>源城</td>\n      <td>161</td>\n      <td>161</td>\n      <td>4室</td>\n      <td>装修交付,学校,购物中心,配套纯熟</td>\n      <td>5800.0</td>\n    </tr>\n    <tr>\n      <th>226</th>\n      <td>中心华府</td>\n      <td>源城</td>\n      <td>114</td>\n      <td>138</td>\n      <td>3室,4室</td>\n      <td>车位充足,装修交付,大户型</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>227</th>\n      <td>华达国际</td>\n      <td>东源</td>\n      <td>115.73</td>\n      <td>141.51</td>\n      <td>3室,4室</td>\n      <td>高绿化率,学校,大户型</td>\n      <td>6100.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>228 rows × 7 columns</p>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_new_house = pd.read_csv('../static/data/data.csv')\n",
    "df_new_house['region'] = df_new_house['address'].apply(\n",
    "    lambda x: re.match('\\[(.*?)\\]', x).group(1))\n",
    "df_new_house['region'] = df_new_house['region'].apply(\n",
    "    lambda x:  x[:int(len(x)/2)])\n",
    "df_new_house['region'] = df_new_house['region'].apply(\n",
    "    lambda x:  x[:2])\n",
    "df_new_house['huxing'] = df_new_house['huxing'].apply(lambda x: \"无\" if pd.isnull(x) else x)\n",
    "df_new_house['house_type'] = df_new_house['huxing'].apply(\n",
    "    lambda x: x.split('建筑面积：')[0][:-1] if len(x.split('建筑面积：')) == 2 else \"无\")\n",
    "df_new_house['area_min'] = df_new_house['huxing'].apply(\n",
    "    lambda x: x.split('建筑面积：')[1][:-1] if len(x.split('建筑面积：')) == 2 else x.split('建筑面积：')[0][:-1])\n",
    "df_new_house['area_max'] = df_new_house['huxing'].apply(\n",
    "    lambda x: x.split('建筑面积：')[1][:-1] if len(x.split('建筑面积：')) == 2 else x.split('建筑面积：')[0][:-1])\n",
    "df_new_house['area_min'] = df_new_house['area_min'].apply(lambda x: x.split('-')[0])\n",
    "df_new_house['area_max'] = df_new_house['area_max'].apply(\n",
    "    lambda x: x.split('-')[1] if len(x.split('-')) == 2 else x.split('-')[0])\n",
    "df_new_house['area_min'] = df_new_house['area_min'].apply(lambda x: \"0\" if x == '' else x)\n",
    "df_new_house['area_max'] = df_new_house['area_max'].apply(lambda x: \"0\" if x == '' else x)\n",
    "df_new_house['house_type'] = df_new_house['house_type'].apply(lambda x: \"无\" if x == '' else x)\n",
    "df_new_house['tag'] = df_new_house['tag'].apply(lambda x: \"无\" if pd.isnull(x) else x)\n",
    "df_new_house['price'] = df_new_house['price'].apply(lambda x: 0 if pd.isnull(x) else x)\n",
    "df_new_house['unit_price'] = df_new_house['price']\n",
    "df_new_house = df_new_house[['name', 'region', 'area_min', 'area_max', 'house_type', 'tag', 'unit_price']]\n",
    "df_new_house.to_csv('../static/data/pretreatment_NH_data.csv',index=None)\n",
    "df_new_house"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-20T05:41:06.399590700Z",
     "start_time": "2023-11-20T05:41:06.371765700Z"
    }
   },
   "id": "f4a8f06d96b1e909"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
